package com.app.freshmenapp6;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONObject;

public class NewsDetailActivity extends AppCompatActivity {
    TextView mTvTitle,mTvData,mTvContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        initData();
        initView();//放到initView之后，这样就可以直接操作initView中所获取的控件
        initEvent();

    }

    private void initEvent() {

    }

    private void initView() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvData =(TextView) findViewById(R.id.dete);
        mTvContent =(TextView) findViewById(R.id.tv_content);

    }

    private void initData() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url= "http://10.1.63.198:8080/freshmenapp/news/findone/1";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                MyBean mybean = gson.fromJson(jsonObject.toString(),MyBean.class);

                mTvTitle.setText(mybean.getData().getTitle());
                mTvData.setText(mybean.getData().getDate());
                mTvContent.setText(mybean.getData().getContent());

                Toast.makeText(NewsDetailActivity.this,"成功",Toast.LENGTH_SHORT).show();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(NewsDetailActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }
}
